package com.gemme.gemme.gwc;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.FapiaoContentMsgDialog;
import com.gemme.gemme.view.FapiaoTaitouTypeDialog;
import com.gemme.gemme.view.FapiaoTypeDialog;
import com.gemme.gemme.view.OnChangeLIstener;
import com.gemme.gemme.view.PersonOrDanweiView;
import com.gemme.gemme.view.RoundCheckBox;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.TvRightEditText;
import com.gemme.gemme.view.TvTvArrow;
import com.gemme.gemme.wode.FaPiaoTaitouActivity;
import com.gemme.gemme.wode.ShopOrderInvoice;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//侧滑删除地址
public class FaPiaoActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fapiao);
        initTop();
        initView();
        getData();
        getIntentData();
    }

    private void getIntentData() {
        try {
            String data = getIntent().getStringExtra("data");
             fpData = JSON.parseObject(data,ShopOrderInvoice.class);
             setData();
        }catch (Exception e){

        }
    }


    TvTvArrow kjfp;
    TvRightEditText fptt;

    TvRightEditText trt_shuihao;
    TvRightEditText trt_khh;
    TvRightEditText trt_yhzh;
    TvRightEditText trt_qydz;
    TvRightEditText trt_qydh;

    ImageView iv_arrow;
    View fapiao_body_taitou;
    PersonOrDanweiView personOrDanweiView;
    boolean isPerson = true;
    View ll_qiye;
    View rl_fapiao_content;
    FapiaoTaitouTypeDialog fapiaoTaitouTypeDialog;
    RoundCheckBox mrtt_checkbox;
    TextView tv_submit;
    TextView tv_bkfp;
    private void initView() {
        fapiaoTaitouTypeDialog = new FapiaoTaitouTypeDialog(mContext);
         kjfp= findViewById(R.id.kjfp);
        fptt = findViewById(R.id.fptt);

        trt_shuihao = findViewById(R.id.trt_shuihao);
        trt_khh = findViewById(R.id.trt_khh);
        trt_yhzh = findViewById(R.id.trt_yhzh);
        trt_qydz = findViewById(R.id.trt_qydz);
        trt_qydh = findViewById(R.id.trt_qydh);

        iv_arrow = findViewById(R.id.iv_arrow);
        fapiao_body_taitou = findViewById(R.id.fapiao_body_taitou);
        personOrDanweiView = findViewById(R.id.personOrDanweiView);


        ll_qiye = findViewById(R.id.ll_qiye);
        rl_fapiao_content = findViewById(R.id.rl_fapiao_content);


        mrtt_checkbox = findViewById(R.id.mrtt_checkbox);
        tv_submit = findViewById(R.id.tv_submit);


        tv_bkfp = findViewById(R.id.tv_bkfp);

        RoundCheckBox spmx_checkbox = findViewById(R.id.spmx_checkbox);
        spmx_checkbox.setChecked(true);
        spmx_checkbox.setEnabled(false);
        kjfp.setLeftText("开具发票").setRightText("增值税电子普通发票");
        fptt.setLeftText("发票抬头").setRightHintText("填写抬头名称，默认为个人").setArrowVisible(View.VISIBLE);
        trt_shuihao.setLeftText("税号").setRightHintText("纳税人识别号或社会统一征信代码");
        trt_khh.setLeftText("开户银行").setRightHintText("选填");
        trt_yhzh.setLeftText("银行账号").setRightHintText("选填");
        trt_qydz.setLeftText("企业地址").setRightHintText("选填");
        trt_qydh.setLeftText("企业电话").setRightHintText("选填");

        iv_arrow.setOnClickListener(this);
        isPerson = personOrDanweiView.getIsPerson();
        changeState();
        setIsPersonListener();
        changeTaitouState();

        kjfp.setOnClickListener(this);
        rl_fapiao_content.setOnClickListener(this);
        tv_submit.setOnClickListener(this);


        //跳转发票抬头界面
        fptt.getArrow().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                startActivity(new Intent(mContext,FaPiaoTaitouActivity.class));


                fapiaoTaitouTypeDialog.addonSelect(onSelect);
                fapiaoTaitouTypeDialog.showDialog();
                if(list != null)
                fapiaoTaitouTypeDialog.setAllData(list);
            }
        });

        tv_bkfp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    ShopOrderInvoice fpData;
    FapiaoTaitouTypeDialog.OnSelect onSelect = new FapiaoTaitouTypeDialog.OnSelect() {
        @Override
        public void onSelect(ShopOrderInvoice shopOrderInvoice) {
            //拿到数据
            //

            fpData = shopOrderInvoice;
            setData();
        }
    };

    private void setData() {
        if (fpData == null)
            return;

         isPerson = fpData.getInvoiceType() == 0 ? true : false;
         changeState();
         if(isPerson){
             fptt.setRightText(fpData.getInvoiceTitle() + "");
         }else{
             fptt.setRightText(fpData.getInvoiceTitle() + "");
             trt_shuihao.setRightText(fpData.getInvoiceTaxNo() + "");
             trt_khh.setRightText(fpData.getVatBankName() + "");
             trt_yhzh.setRightText(fpData.getVatBankAccount() + "");
             trt_qydz.setRightText(fpData.getVatCompanyAddress() + "");
             trt_qydh.setRightText(fpData.getVatTelphone() + "");
         }

        boolean isDef = fpData.getDefaultType() == 0 ? true : false;
        mrtt_checkbox.setChecked(isDef);
    }


    private void setIsPersonListener() {
        personOrDanweiView.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                isPerson = personOrDanweiView.getIsPerson();
                changeState();
            }
        });
    }


    private void changeState() {
        if(isPerson){
//            iv_arrow.setBackgroundResource(R.drawable.pc_arrow_dowm);
//            fapiao_body_taitou.setVisibility(View.GONE);
            trt_shuihao.setVisibility(View.GONE);
            iv_arrow.setVisibility(View.GONE);
        }else{
//            iv_arrow.setBackgroundResource(R.drawable.pc_arrow_up);
            trt_shuihao.setVisibility(View.VISIBLE);
            iv_arrow.setVisibility(View.VISIBLE);
        }

        personOrDanweiView.setstate(isPerson);
    }

    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("开具发票");
    }

    boolean isOpen = false;

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_arrow:
                isOpen = !isOpen;
                changeTaitouState();
                break;
            case R.id.kjfp:
                new FapiaoTypeDialog().showDialog(mContext);
                break;

            case R.id.rl_fapiao_content:
                new FapiaoContentMsgDialog().showDialog(mContext);
                break;

            case R.id.tv_submit:
                //添加新的发票

               judge();
                break;
        }
    }

    private void judge() {
        if(fpData == null){
            addtoServer();
            return;
        }

        Long id = fpData.getId();
        if(id != null && id >= 0){
            //修改数据
            putData();
        }else{
            //添加数据
            addtoServer();
        }

    }

    private void putData() {
        String fpttstr = fptt.getContent();
        if(TextUtils.isEmpty(fpttstr)){
            Toast.makeText(mContext, "发票抬头不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(fpData == null){
            fpData = new ShopOrderInvoice();
        }

        fpData.setIsVat(0);
        fpData.setInvoiceType(isPerson ? 0 : 1);
        fpData.setInvoiceTitle(fpttstr);
        fpData.setDefaultType(mrtt_checkbox.isChecked() ? 0 : 1);
        if(isPerson){

        }else{
            fpData.setInvoiceTaxNo(trt_shuihao.getContent() + "");
            fpData.setVatCompanyName(fpttstr);
            fpData.setVatCompanyAddress(trt_qydz.getContent() + "");
            fpData.setVatTelphone(trt_qydh.getContent() + "");
            fpData.setVatBankName(trt_khh.getContent() + "");
            fpData.setVatBankAccount(trt_yhzh.getContent() + "");
        }


        String jsonString = JSON.toJSONString(fpData);
        String url = DataManager.BaseUrl + "mall/shoporderinvoice";
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.PUT_TYPE, jsonString, headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    System.out.println();
                    Intent intent = new Intent();
                    intent.putExtra("data",JSON.toJSONString(fpData));
                    setResult(1,intent);
                    finish();
                }catch (Exception e){

                }
            }
        });
    }

    /*    fptt
                trt_shuihao
        trt_khh
                trt_yhzh
        trt_qydz
                trt_qydh*/
    private void addtoServer() {
        String fpttstr = fptt.getContent();
        if(TextUtils.isEmpty(fpttstr)){
            Toast.makeText(mContext, "发票抬头不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(fpData == null){
            fpData = new ShopOrderInvoice();
        }

        fpData.setIsVat(0);
        fpData.setInvoiceType(isPerson ? 0 : 1);
        fpData.setInvoiceTitle(fpttstr);
        fpData.setDefaultType(mrtt_checkbox.isChecked() ? 0 : 1);
        if(isPerson){

        }else{
            fpData.setInvoiceTaxNo(trt_shuihao.getContent() + "");
            fpData.setVatCompanyName(fpttstr);
            fpData.setVatCompanyAddress(trt_qydz.getContent() + "");
            fpData.setVatTelphone(trt_qydh.getContent() + "");
            fpData.setVatBankName(trt_khh.getContent() + "");
            fpData.setVatBankAccount(trt_yhzh.getContent() + "");
        }


        String jsonString = JSON.toJSONString(fpData);
        String url = DataManager.BaseUrl + "mall/shoporderinvoice";
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.POST_TYPE, jsonString, headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    Long id = (new JSONObject(response)).getLong("data");
                    if (id != null)
                        fpData.setId(id);
                    System.out.println();
                    Intent intent = new Intent();
                    intent.putExtra("data",JSON.toJSONString(fpData));
                    setResult(1,intent);
                    finish();
                }catch (Exception e){

                }
            }
        });
    }

    private void changeTaitouState() {
        if (isOpen){
            iv_arrow.setBackgroundResource(R.drawable.pc_arrow_up);
            ll_qiye.setVisibility(View.VISIBLE);
        }else{
            iv_arrow.setBackgroundResource(R.drawable.pc_arrow_dowm);
            ll_qiye.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 1000 && data != null){
            String json = data.getStringExtra("data");
            ShopOrderInvoice shopOrderInvoice = JSON.parseObject(json, ShopOrderInvoice.class);
            System.out.println( "FaPiaoActivity : " + json);
            fapiaoTaitouTypeDialog.addData(shopOrderInvoice);
            fpData = shopOrderInvoice;
            setData();
        }
    }
    List<ShopOrderInvoice> list;

    private void getData() {
        String url = DataManager.BaseUrl + "mall/shoporderinvoice/getShopOrderInvoice";
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.GET_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {

                    list = JSON.parseArray((new JSONObject(response)).getString("data"), ShopOrderInvoice.class);
                    if(list == null)
                        return;

                    System.out.println();
                }catch (Exception e){

                }
            }
        });
    }
}